package com.android.killtencent.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.android.killtencent.BaseApp;
import com.android.killtencent.R;
import com.android.killtencent.task.LoginTask;
import com.android.pc.ioc.inject.InjectInit;
import com.android.pc.ioc.inject.InjectLayer;
import com.android.pc.ioc.inject.InjectListener;
import com.android.pc.ioc.inject.InjectMethod;
import com.android.pc.ioc.inject.InjectView;
import com.android.pc.ioc.verification.Rule;
import com.android.pc.ioc.verification.Validator;
import com.android.pc.ioc.verification.annotation.Password;
import com.android.pc.ioc.verification.annotation.Regex;
import com.android.pc.ioc.view.listener.OnClick;
import com.android.pc.util.Handler_TextStyle;

/**
 * 作者:	Rose
 * 业务名:    登陆界面
 * 功能说明: 登陆逻辑处理
 * 编写日期:    2015-02-10
 * 验证框架的使用
 * 第一步
 * validator.setValidationListener(this);
 * validator.validate();
 */
@InjectLayer(value = R.layout.activity_login_layout, isFull = true, isTitle = true)
public class LoginActivity extends BaseActivity implements Validator.ValidationListener {
    @InjectView(R.id.login_account_cet)
    @Regex(message = "请使用字母加数字组合", pattern = "[a-zA-Z0-9_]{6,15}", order = 6)
    private EditText account;
    @InjectView(R.id.login_psw_cet)
    @Password(message = "请输入密码", order = 1, minLength = 6, maxLength = 18)
    private EditText passWordEt;
    private Validator validator;
    private String userName;
    private String passWord;


    @InjectInit
    private void init() {
        Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.login_anim);
        anim.setFillAfter(true);
        //实例化验证类
        validator = new Validator(this);
        validator.setValidationListener(this);
    }

    @InjectMethod(@InjectListener(ids = {R.id.login_btn, R.id.login_register_tv, R.id.login_findpsw_tv}, listeners = {OnClick.class}))
    public void setBtnOnclick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                validator.validate();
                break;
            case R.id.login_register_tv:
                startActivity(new Intent(LoginActivity.this , RegisterActivity.class));
                break;
            case R.id.login_findpsw_tv:
                Toast.makeText(getApplicationContext(), "忘记了就重新申请一个，又不收钱的事", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 验证成功
     */
    @Override
    public void onValidationSucceeded() {
        userName = account.getText().toString().trim();
        passWord = passWordEt.getText().toString().trim();
        new LoginTask(userName, passWord, this).execute();
    }

    /**
     * 验证失败
     *
     * @param view
     * @param rule
     */
    @Override
    public void onValidationFailed(View view, Rule<?> rule) {
        String message = rule.getFailureMessage();
        if (view instanceof EditText) {
            view.requestFocus();
            Handler_TextStyle handler_TextStyle = new Handler_TextStyle();
            handler_TextStyle.setString(message);
            handler_TextStyle.setBackgroundColor(Color.RED, 0, message.length());
            ((EditText) view).setError(handler_TextStyle.getSpannableString());
        } else {
            BaseApp.getInstance().showToast(message);
        }
    }
}
